using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using C4F.DevKit.PictureAcquisition;

namespace PictureAcquisitionApplication
{
    public partial class DeviceItemsDetailsInThumbnails : Form
    {
        #region Globals
        PictureAcquisitionManager pictureAquisitionManager = null;
        string deviceIdNew = string.Empty;
        List<Image> deviceItems = new List<Image>();
        List<PictureBox> lstPictureBox = new List<PictureBox>();
        List<Label> lstLabel = new List<Label>();
        List<CheckBox> lstCheckBox = new List<CheckBox>();
        #endregion

        #region Helper Method
        public DeviceItemsDetailsInThumbnails()
        {
            InitializeComponent();
        }

        public DeviceItemsDetailsInThumbnails(PictureAcquisitionManager manager, string deviceId)
        {
            deviceIdNew = deviceId;
            pictureAquisitionManager = manager;
            InitializeComponent();
            GetDeviceItemsDetailsInThumbnails(deviceIdNew);
        }

        private void GetDeviceItemsDetailsInThumbnails(string deviceIdNew)
        {
            try
            {
                Cursor.Current = Cursors.WaitCursor;
                deviceItems = pictureAquisitionManager.GetThumbnails(deviceIdNew);
                RenderingThumbNails();
                Cursor.Current = Cursors.Default;
            }
            catch (ArgumentNullException ex)
            {
                Cursor.Current = Cursors.Default;
                MessageBox.Show(ex.Message, "Picture Acquisition", MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
            }
            catch (ArgumentException ex)
            {
                Cursor.Current = Cursors.Default;
                MessageBox.Show(ex.Message, "Picture Acquisition", MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
            }
            catch (PictureAcquisitionException ex)
            {
                Cursor.Current = Cursors.Default;
                MessageBox.Show(ex.Message, "Picture Acquisition", MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
            }

        }

        private void RenderingThumbNails()
        {
            int i = 0;
            foreach (Image dev in deviceItems)
            {

                lstPictureBox.Add(new PictureBox());
                lstLabel.Add(new Label());
                lstCheckBox.Add(new CheckBox());

                if (i % 2 != 0)
                {
                    ((System.ComponentModel.ISupportInitialize)(lstPictureBox[i])).BeginInit();
                    lstPictureBox[i].Location = new System.Drawing.Point(10 + 205, 10 + (i / 2) * 130);

                    lstLabel[i].Location = new System.Drawing.Point(10 + 205, 115 + (i / 2) * 130);
                    lstLabel[i].Size = new System.Drawing.Size(205, 13);
                    lstLabel[i].Text = dev.Tag.ToString();

                    lstCheckBox[i].Location = new System.Drawing.Point(140 + 205, 48 + 10 + (i / 2) * 130);
                    lstCheckBox[i].Text = string.Empty;

                    //To contract the size of the checkbox so that it should not overlap to the adjacent picturebox.
                    lstCheckBox[i].Size = new Size(20, 20);

                    //lstPictureBox[i].Name = "pictureBox1";
                    lstPictureBox[i].Size = new System.Drawing.Size(100, 100);
                    lstPictureBox[i].Image = dev;
                    this.pnlDeviceItemInThumbNails.Controls.Add(lstPictureBox[i]);
                    this.pnlDeviceItemInThumbNails.Controls.Add(lstLabel[i]);
                    this.pnlDeviceItemInThumbNails.Controls.Add(lstCheckBox[i]);
                    ((System.ComponentModel.ISupportInitialize)(lstPictureBox[i])).EndInit();

                }
                if (i % 2 == 0)
                {
                    ((System.ComponentModel.ISupportInitialize)(lstPictureBox[i])).BeginInit();
                    lstPictureBox[i].Location = new System.Drawing.Point(10, 10 + (i / 2) * 130);

                    lstLabel[i].Location = new System.Drawing.Point(10, 115 + (i / 2) * 130);
                    lstLabel[i].Size = new System.Drawing.Size(205, 13);
                    lstLabel[i].Text = dev.Tag.ToString();

                    lstCheckBox[i].Location = new System.Drawing.Point(140, 58 + (i / 2) * 130);
                    lstCheckBox[i].Text = string.Empty;

                    //To contract the size of the checkbox so that it should not overlap to the adjacent picturebox.
                    lstCheckBox[i].Size = new Size(20, 20);

                    //lstPictureBox[i].Name = "pictureBox1";
                    lstPictureBox[i].Size = new System.Drawing.Size(100, 100);
                    lstPictureBox[i].Image = dev;
                    this.pnlDeviceItemInThumbNails.Controls.Add(lstPictureBox[i]);
                    this.pnlDeviceItemInThumbNails.Controls.Add(lstLabel[i]);
                    this.pnlDeviceItemInThumbNails.Controls.Add(lstCheckBox[i]);
                    ((System.ComponentModel.ISupportInitialize)(lstPictureBox[i])).EndInit();
                }
                i++;

            }
        }

        private void RenderingThumbNailsFromPictureBox()
        {
            int i = 0;
            for (; i < lstPictureBox.Count;)
            {

                if (i % 2 != 0)
                {
                    lstPictureBox[i].Location = new System.Drawing.Point(215, 10 + (i / 2) * 130);

                    lstLabel[i].Location = new System.Drawing.Point(215, 115 + (i / 2) * 130);
                    
                    lstCheckBox[i].Location = new System.Drawing.Point(345, 58 + (i / 2) * 130);
                    
                    this.pnlDeviceItemInThumbNails.Controls.Add(lstPictureBox[i]);
                    this.pnlDeviceItemInThumbNails.Controls.Add(lstLabel[i]);
                    this.pnlDeviceItemInThumbNails.Controls.Add(lstCheckBox[i]);
                    ((System.ComponentModel.ISupportInitialize)(lstPictureBox[i])).EndInit();

                }
                if (i % 2 == 0)
                {
                    ((System.ComponentModel.ISupportInitialize)(lstPictureBox[i])).BeginInit();
                    lstPictureBox[i].Location = new System.Drawing.Point(10, 10 + (i / 2) * 130);

                    lstLabel[i].Location = new System.Drawing.Point(10, 115 + (i / 2) * 130);
                    
                    lstCheckBox[i].Location = new System.Drawing.Point(140, 58 + (i / 2) * 130);
                    
                    this.pnlDeviceItemInThumbNails.Controls.Add(lstPictureBox[i]);
                    this.pnlDeviceItemInThumbNails.Controls.Add(lstLabel[i]);
                    this.pnlDeviceItemInThumbNails.Controls.Add(lstCheckBox[i]);
                    ((System.ComponentModel.ISupportInitialize)(lstPictureBox[i])).EndInit();
                }
                i++;

            }
        }

        private void DeleteDeviceItemInThumbnails(string deviceIdNew)
        {
            try
            {                
                int i = 0;
                int SelectedCheckBoxIndex = 0;
                List<Int32> lstOfSelectedCheckBox = new List<Int32>();
                List<Int32> lstOfNotSelectedCheckBox = new List<Int32>();
                for (i = 0; i < lstCheckBox.Count; i++ )
                {
                    if (lstCheckBox[i].Checked)
                        lstOfSelectedCheckBox.Add(i);
                    else
                        lstOfNotSelectedCheckBox.Add(i);
                }
                Cursor.Current = Cursors.WaitCursor;
                for (SelectedCheckBoxIndex = 0; SelectedCheckBoxIndex < lstOfSelectedCheckBox.Count;
                                                    SelectedCheckBoxIndex++)
                {
                    string itemName = lstLabel[lstOfSelectedCheckBox[SelectedCheckBoxIndex]].Text;                    
                    pictureAquisitionManager.DeleteItem(itemName, deviceIdNew);
                  
                }
                DeletePictureDataFromListBoxex(lstOfNotSelectedCheckBox);                
                RenderingThumbNailsFromPictureBox();
                Cursor.Current = Cursors.Default;                
            }
            catch (ArgumentNullException ex)
            {
                Cursor.Current = Cursors.Default;
                MessageBox.Show(ex.Message, "Picture Acquisition", MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
            }
            catch (ArgumentException ex)
            {
                Cursor.Current = Cursors.Default;
                MessageBox.Show(ex.Message, "Picture Acquisition", MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
            }
            catch (PictureAcquisitionException ex)
            {
                Cursor.Current = Cursors.Default;
                MessageBox.Show(ex.Message, "Picture Acquisition", MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
            }
        }

        private void DeletePictureDataFromListBoxex(List<Int32> remainingPhotos)
        {
            List<PictureBox> templstPictureBox = new List<PictureBox>();
            List<Label> templstLabel = new List<Label>();
            List<CheckBox> templstCheckBox = new List<CheckBox>();

            foreach (int remainingIndex in remainingPhotos)
            {

                templstPictureBox.Add(lstPictureBox[remainingIndex]);
                templstLabel.Add(lstLabel[remainingIndex]);
                templstCheckBox.Add(lstCheckBox[remainingIndex]);
        }
            int totalItem = lstPictureBox.Count;
            for (int i = 0; i < totalItem ; i++)
            {
                pnlDeviceItemInThumbNails.Controls.Remove(lstPictureBox[i]);
                pnlDeviceItemInThumbNails.Controls.Remove(lstLabel[i]);
                pnlDeviceItemInThumbNails.Controls.Remove(lstCheckBox[i]);
            }

            lstPictureBox = templstPictureBox;
            lstLabel = templstLabel;
            lstCheckBox = templstCheckBox;
        }

        private void AcquireSelectedDeviceItemsInThumbnails(string deviceIdNew)
        {
            try
            {
                int i = 0;
                int SelectedCheckBoxIndex = 0;
                List<Int32> lstOfSelectedCheckBox = new List<Int32>();
                List<string> lstOfSelectedItemNames = new List<string>();
                for (i = 0; i < lstCheckBox.Count; i++)
                {
                    if (lstCheckBox[i].Checked)
                        lstOfSelectedCheckBox.Add(i);
                }

                for (SelectedCheckBoxIndex = 0; SelectedCheckBoxIndex < lstOfSelectedCheckBox.Count;
                                                    SelectedCheckBoxIndex++)
                {
                    lstOfSelectedItemNames.Add(lstLabel[lstOfSelectedCheckBox[SelectedCheckBoxIndex]].Text);                    
                }
                FolderBrowserDialog fileBrowserDialog = new FolderBrowserDialog();
                fileBrowserDialog.Description = "Choose folder to import selected files";
                fileBrowserDialog.ShowNewFolderButton = true;

                if (DialogResult.OK == fileBrowserDialog.ShowDialog())
                {
                    Cursor.Current = Cursors.WaitCursor;
                    pictureAquisitionManager.AcquireItems(deviceIdNew, lstOfSelectedItemNames, fileBrowserDialog.SelectedPath + "\\");
                    Cursor.Current = Cursors.Default;
                }

            }
            catch (ArgumentNullException ex)
            {
                Cursor.Current = Cursors.Default;
                MessageBox.Show(ex.Message, "Picture Acquisition", MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
            }
            catch (ArgumentException ex)
            {
                Cursor.Current = Cursors.Default;
                MessageBox.Show(ex.Message, "Picture Acquisition", MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
            }
            catch (PictureAcquisitionException ex)
            {
                Cursor.Current = Cursors.Default;
                MessageBox.Show(ex.Message, "Picture Acquisition", MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
            }
        }

        #endregion

        #region Events Handler
        private void btnDeleteDeviceItemsInThumbnails_Click(object sender, EventArgs e)
        {
            DeleteDeviceItemInThumbnails(deviceIdNew);
        }

        private void btnDeviceItemDetailsInThumbnailsImport_Click(object sender, EventArgs e)
        {            
            AcquireSelectedDeviceItemsInThumbnails(deviceIdNew);            
        }

        private void btnCloseDeviceItemsInThumbnails_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void DeviceItemsDetailsInThumbnails_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
                this.Close();
        }
        #endregion
    }
}